﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BaseData;
using BaseData.Common;
using ShiziEntity.User;

namespace ShizirDAL.User
{
    /// <summary>
    /// 用户基类数据层
    /// </summary>
    public class UserBaseData
    {
        /// <summary>
        /// 判断用户是否存在
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>存在true，不存在返回false</returns>
        public bool Exists(string userName)
        {
            string sql = "select count(1) from userbase where username = @username";
            SqlParameter p = new SqlParameter("@username", SqlDbType.VarChar, 50);
            p.Value = userName;
            return DataConfig.Exists(BusinessType.ShiZirRead, sql, p);
        }

        /// <summary>
        /// 获取用户信息实体
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>用户信息实体</returns>
        public UserBase GetUserBaseEntity(string userName)
        {
            string sql = "select * from userbase where username = @username ";
            SqlParameter p = new SqlParameter("@username", SqlDbType.NVarChar, 50);
            p.Value = userName;
            DataTable dt = DataConfig.QueryRecords(BusinessType.ShiZirRead, sql, p);
            return this.GetUserEntity(dt);
        }

        /// <summary>
        /// 保存用户基类
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <param name="userID">用户编号</param>
        /// <returns>操作结果</returns>
        public bool SaveUserBase(UserBase user, ref int userID)
        {
            bool isSucc = false;
            string sql = user.GetInsertSql();
            object obj = DataConfig.GetSingle(BusinessType.ShiZirWrite, sql.ToString());
            if (obj != null)
            {
                if (int.TryParse(obj.ToString(), out userID))
                {
                    isSucc = true;
                }
            }
            return isSucc;
        }

        /// <summary>
        /// 从datatable中获取用户实体
        /// </summary>
        /// <param name="dtUserList">用户列表</param>
        /// <returns>用户实体</returns>
        private UserBase GetUserEntity(DataTable dtUserList)
        {
            UserBase user = null;
            if (dtUserList != null && dtUserList.Rows.Count > 0)
            {
                user = new UserBase();
                foreach (DataRow dr in dtUserList.Rows)
                {
                    user.UserID = TableHelper.GetIntValueFromDr(dr, "ID");
                    user.UserName = TableHelper.GetStringValueFromDr(dr, "UserName");
                    user.Password = TableHelper.GetStringValueFromDr(dr, "Password");
                    user.CityName = TableHelper.GetStringValueFromDr(dr, "CityName");
                    user.RealName = TableHelper.GetStringValueFromDr(dr, "RealName");
                    user.Email = TableHelper.GetStringValueFromDr(dr, "eMail");
                    user.RegistDate = TableHelper.GetDateTimeValueFromDr(dr, "RegistDate");
                    user.UserType = TableHelper.GetIntValueFromDr(dr, "UserType");
                    user.Referrer = TableHelper.GetStringValueFromDr(dr, "Referrer");
                }
            }
            return user;
        }
    }
}